<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Validator;
use Input;
use App\Models\Admin;
use Session;
class LoginController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		//
	}

	public function doLogin()
	{
		$account = Input::get('username', null);
		$password = Input::get('password', null);
		$rule = [
			'username'	=>	'required',
			'password'	=>	'required'
		];
		$msg = [
			'username.required'		=>	'请输入用户名',
			'password.required'		=>	'请输入密码'
		];
		$validator = Validator::make(Input::all(), $rule, $msg);
		if($validator->passes()){
			$model = Admin::where("account", $account);
			if($model->count()){
				$ps = $model->get()[0]->password;
				if($password == $ps){
					$adm = $model->get()[0];

					$logData = [
						'last_login'	=> 	date('Y-m-d H:i:s', time()),
					];
					$model->update($logData);
					Session::put("admin", $adm);

					return $this->json([]);
				}else{
					return self::error(self::USERNAME_PASSWORD_IS_WRONG);
				}
			}else{
				return self::error(self::USERNAME_PASSWORD_IS_WRONG);
			}
		}else{
			$messages = $validator->messages();
			return self::error('', $messages->first());
		}
	}

	public function loginOut()
	{
		if(Session::has('admin')){
			Session::forget('admin');
			return Redirect::to('login');
		}
	}


	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}
